﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cms.Core.Application.IService.ModuleInfo;
using Cms.Core.Application.Service.ModuleInfo.Dto;
using Cms.Core.AspNetMvc.Mvc;
using Cms.Core.Infrastructure;
using Cms.Core.Infrastructure.Ui;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Cms.Core.Web.Controllers
{
    /// <summary>
    /// 模块信息接口
    /// </summary>
    [Display("模块信息")]
    public class ModuleController : WebApiController
    {

        private readonly IModuleService _moduleService = null;

        public ModuleController(IModuleService moduleService) => _moduleService = moduleService;

        /// <summary>
        /// 异步添加或者更新模块
        /// </summary>
        /// <param name="input">模块Dto</param>
        /// <returns></returns>
        [Display("异步添加或者更新模块"), HttpPost]

        public async Task<AjaxResult> AddOrUpdateModuleAsync([ModelBinder(Name = "Id,Name,LinkUrl,Code,OrderSort,Description,IsEnabled,Group")][FromBody]ModuleDto input)
        {

            var result = await _moduleService.UpdateOrAddModuleAsync(input);
            return result.ToAjaxResult();
        }

        /// <summary>
        /// 异步得到模块分页数据
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        [Display("异步得到模块分页数据"), HttpPost]
        public async Task<PageData> GetModulePageDataAsync([FromBody]PageRequestData requestData)
        {

            var pageData = await _moduleService.GetModulePageDataAsync(requestData);
            return pageData.ToPageData();
        }

        /// <summary>
        /// 异步软删除模块
        /// </summary>
        [Display("异步软删除模块"), HttpGet]
        public async Task<AjaxResult> SoftDeleteModuleAsync(Guid id)
        {
            return (await _moduleService.SoftDeleteModuleAsync(id)).ToAjaxResult();
        }


        /// <summary>
        /// 异步得到所有模块转成下拉项
        /// </summary>
        [Display("异步得到所有模块转成下拉项"), HttpGet]
        public async Task<AjaxResult> GetAllModuleToSelectListItemAsync()
        {
            return (await _moduleService.GetAllModuleToSelectItem()).ToAjaxResult();
        }
    }
}